<template>
  <div class="timeline-container">
    <div class="timeline-header" v-if="title">
      <h2 class="text-2xl font-bold text-gray-800 mb-4">{{ title }}</h2>
    </div>
    <div class="timeline-items">
      <div 
        v-for="(item, index) in items" 
        :key="item.id || index"
        class="timeline-item"
        :class="{ 'timeline-item-right': isRight(index) }"
      >
        <div class="timeline-dot" :class="item.dotClass || 'bg-primary'"></div>
        <div class="timeline-content">
          <div class="timeline-date text-sm font-medium text-gray-500 mb-1">
            {{ item.date }}
          </div>
          <div class="timeline-title text-lg font-semibold text-gray-800 mb-2">
            {{ item.title }}
          </div>
          <div class="timeline-description text-gray-600">
            {{ item.description }}
          </div>
          <div class="timeline-actions mt-3" v-if="item.actions">
            <button 
              v-for="(action, actionIndex) in item.actions" 
              :key="actionIndex"
              class="px-3 py-1 text-sm rounded-md transition-all duration-300"
              :class="action.class || 'bg-primary text-white hover:bg-primary/90'"
              @click="action.onClick ? action.onClick(item) : null"
            >
              {{ action.label }}
            </button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'Timeline',
  props: {
    items: {
      type: Array,
      required: true,
      default: () => []
    },
    title: {
      type: String,
      default: ''
    },
    alternate: {
      type: Boolean,
      default: true
    }
  },
  methods: {
    isRight(index) {
      return this.alternate && index % 2 === 1;
    }
  }
}
</script>

<style scoped>
.timeline-container {
  padding: 20px;
  max-width: 1000px;
  margin: 0 auto;
}

.timeline-items {
  position: relative;
}

.timeline-items::before {
  content: '';
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 2px;
  height: 100%;
  background-color: #e2e8f0;
  z-index: 1;
}

.timeline-item {
  position: relative;
  margin-bottom: 40px;
  display: flex;
  justify-content: flex-end;
  padding-right: 30px;
}

.timeline-item-right {
  justify-content: flex-start;
  padding-right: 0;
  padding-left: 30px;
}

.timeline-dot {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  width: 20px;
  height: 20px;
  border-radius: 50%;
  z-index: 2;
  box-shadow: 0 0 0 4px white;
  transition: all 0.3s ease;
}

.timeline-dot:hover {
  transform: translateX(-50%) scale(1.1);
}

.timeline-content {
  width: calc(50% - 40px);
  background-color: white;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
}

.timeline-content:hover {
  transform: translateY(-5px);
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05);
}

/* 移动端适配 */
@media (max-width: 768px) {
  .timeline-items::before {
    left: 20px;
  }
  
  .timeline-item, .timeline-item-right {
    justify-content: flex-end;
    padding-left: 0;
    padding-right: 0;
    margin-left: 40px;
  }
  
  .timeline-dot {
    left: 0;
    transform: translateX(-50%);
  }
  
  .timeline-content {
    width: 100%;
  }
}
</style>  